<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="50%">
      <el-form
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm form"
        label-width="100px"
      >
        <el-form-item class="item" label="线缆类型:" prop="">
          <el-input
            v-model="form.cableClass"
            placeholder="请输入线缆类型"
          ></el-input>
        </el-form-item>
        <el-form-item class="item" label="规格型号:" prop="">
          <el-input
            v-model="form.cableModel"
            placeholder="请输入规格型号"
          ></el-input>
        </el-form-item>
        <el-form-item class="item" label="长度单位:" prop="">
          <el-input
            v-model="form.lengthUnit"
            placeholder="请输入长度单位"
          ></el-input>
        </el-form-item>
        <el-form-item class="item" label="重量单位:" prop="">
          <el-input
            v-model="form.weightUnit"
            placeholder="请输入重量单位"
          ></el-input>
        </el-form-item>
        <el-form-item class="item" label="生产日期:" prop="">
          <el-date-picker
            v-model="prodDate"
            type="datetime"
            placeholder="选择生产日期"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="item" label="生产厂家:" prop="">
          <el-input
            style="width: 100%"
            v-model="form.producer"
            placeholder="请输入生产厂家"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="loginBth2">取 消</el-button>
        <el-button
          type="primary"
          @click="submit"
          :loading="loading"
          class="loginBth"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCable, getDataDetail, editCable } from "@/common/api";
import { formatISODate } from "@/untils/trans";
export default {
  name: "AddData",
  props: {},
  components: {},
  computed: {
    title() {
      return this.type == "add" ? "新增数据" : "编辑数据";
    },
  },
  data() {
    return {
      dialogFormVisible: false,
      options: [],
      form: {
        cableClass: "",
        cableModel: "",
        lengthUnit: "",
        weightUnit: "",
        producer: "",
      },
      prodDate: "",
      rules: {},
      loading: false,
      type: "add",
      id: "",
    };
  },
  mounted() {},
  created() {},
  methods: {
    getDetail(id) {
      this.id = id;
      getDataDetail({ id }).then((res) => {
        if (res.success) {
          for (let key in this.form) {
            this.form[key] = res.data[key];
            this.prodDate = res.data.prodDate;
          }
        } else {
          this.$errorMsg(res.message);
        }
      });
    },
    submit() {
      if (this.type == "add") {
        let param = {
          ...this.form,
          prodDate: this.prodDate
            ? formatISODate(this.prodDate)
            : this.prodDate,
        };
        this.loading = true;
        addCable(param)
          .then((res) => {
            this.loading = false;
            if (res.success) {
              this.$successMsg("添加成功");
              this.dialogFormVisible = false;
              this.prodDate = "";
              let arr = Object.keys(this.form);
              arr.forEach((i) => (this.form[i] = "")); //表单数据置空
              this.$emit("getTable");
            } else {
              this.$errorMsg(res.message);
            }
          })
          .catch((err) => {
            this.loading = false;
          });
      } else {
        editCable({
          ...this.form,
          id: this.id,
          prodDate: this.prodDate
            ? formatISODate(this.prodDate)
            : this.prodDate,
        })
          .then((res) => {
            this.loading = false;
            if (res.success) {
              this.$successMsg("修改成功");
              this.dialogFormVisible = false;
              let arr = Object.keys(this.form);
              arr.forEach((i) => (this.form[i] = "")); //表单数据置空
              this.prodDate = "";
              this.$emit("getTable");
            } else {
              this.$errorMsg(res.message);
            }
          })
          .catch((err) => {
            this.loading = false;
          });
      }
    },
    cancel() {
      this.dialogFormVisible = false;
      let arr = Object.keys(this.form);
      arr.forEach((i) => (this.form[i] = "")); //表单数据置空
      this.prodDate = "";
    },
  },
};
</script>


<style lang="scss" scoped>
.form {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  //   gap: 20px;
  .item {
    width: 50%;
  }
}
</style>